package com.czh.domain;

import com.czh.enums.AppHttpCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ResponseResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;
    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }
//    public ResponseResult(Integer code, T data) {
//        this.code = code;
//        this.data = data;
//    }
//    public ResponseResult(Integer code, String msg, T data) {
//        this.code = code;
//        this.msg = msg;
//        this.data = data;
//    }
//    public ResponseResult(Integer code, String msg) {
//        this.code = code;
//        this.msg = msg;
//    }
    public static <T> ResponseResult<T> errorResult(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        return result.error(code, msg);
    }
    public static <T> ResponseResult<T> okResult() {
        return new ResponseResult<>();
    }
    public static <T> ResponseResult<T> okResult(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<>();
        return result.ok(code, null, msg);
    }
    public static <T> ResponseResult<T> okResult(T data) {
        ResponseResult<T> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS);
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }
    //有的业务中在错误的同时也需要携带数据
    public static  <T> ResponseResult<T> errorResult(T data,AppHttpCodeEnum enums){
        ResponseResult<T> responseResult = new ResponseResult<>();
        return responseResult.error(enums.getCode(),enums.getMsg(),data);
    }
    public static <T> ResponseResult<T> errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums);
    }
    private static <T> ResponseResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode(),enums.getMsg());
    }
    public ResponseResult<T> error(Integer code, String msg,T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }
    public ResponseResult<T> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }
    public ResponseResult<T> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }
}